package com.example.demo.controller;

import com.example.demo.domain.AgeCount;
import com.example.demo.domain.Volunteer;
import com.example.demo.result.RespBean;
import com.example.demo.service.VolunteerServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.sql.Timestamp;
import java.util.List;

@RestController
@RequestMapping(value = "/VolunteerManage")
@CrossOrigin
public class VolunteerController {
    @Autowired
    private VolunteerServiceImpl volunteerService;

    @GetMapping(value = "/findAll")
    @ResponseBody
    public RespBean findAll(){
        List<Volunteer> volunteers;
        volunteers = volunteerService.findAll();
        return RespBean.success("success",volunteers);
    }

    @GetMapping(value = "/findOneVolunteer")
    @ResponseBody
    public RespBean findOneElder(@RequestParam("volunteer_id") int id){
        Volunteer volunteer;
        volunteer = volunteerService.selectVolunteer(id);
        return RespBean.success("success",volunteer);
    }

    @GetMapping(value = "/deleteOneVolunteer")
    @ResponseBody
    public RespBean deleteVolunteer(@RequestParam("volunteer_id") int id){
        volunteerService.deleteVolunteer(id);
        return RespBean.success("success");
    }

    @PostMapping(value = "/addVolunteer")
    @ResponseBody
    public RespBean addVolunteer(@RequestBody @Valid Volunteer volunteer){
        Timestamp d = new Timestamp(System.currentTimeMillis());
        volunteer.setCreated(d.toString());
        volunteer.setImgset_dir("/img/volunteer");
        volunteer.setProfile_photo("/volunteer/profile/");
        volunteerService.addVolunteer(volunteer);
        return RespBean.success("success");
    }

    @PostMapping(value = "/updateVolunteer")
    @ResponseBody
    public RespBean updateVolunteer(@RequestBody @Valid Volunteer volunteer){
        volunteerService.updateVolunteer(volunteer);
        return RespBean.success("success");
    }

    @GetMapping(value = "/selectByAge")
    @ResponseBody
    public RespBean SelectAge(){
        AgeCount ageCount = new AgeCount();
        int[] age = new int[4];
        age[0] = volunteerService.selectAge(1,59);
        age[1] = volunteerService.selectAge(60,69);
        age[2] = volunteerService.selectAge(70,79);
        age[3] = volunteerService.selectAge(80,150);
        ageCount.setAge(age);
        return RespBean.success("success",ageCount);
    }
}
